import api from './api'
// 登录
export const login = userModel => {
	return api.post('/users/login', userModel).then(res => {
		// 判断是否请求成功
		if (res.data.status == 1) {
			throw res.data.message
		} else {
			localStorage.setItem('token', 'Bearer ' + res.data.token)
			localStorage.setItem('userName', userModel.userName)
			AMessage.success('登录成功!')
		}
	})
}
// 注册
export const register = userModel => {
	return api.post('/users/register', userModel).then(res => {
		// 判断是否请求成功
		if (res.data.status == 1) {
			throw res.data.errors[0].msg
		} else {
			AMessage.success('注册成功!')
		}
	})
}
// 获取个人信息
export const getUserInfo = () => {
	return api.get('/users/userInfo').then(res => {
		localStorage.setItem('avatarUrl', res.data.data.userAvatar)
		return res.data.data
	})
}
// 修改个人信息
export const changeInfo = data => {
	return api.post('/users/changeInfo', data).then(res => {
		return res.data
	})
}

// 获取用户历史评论
export const getUserComment = id => {
	return api.get(`/index/comment/${id}`).then(res => {
		return res.data
	})
}
// 修改密码
export const setChangePassword = changePassModel => {
	return api.post('/users/findPassword', changePassModel).then(res => {
		if (res.data.status === 1) {
			res.data.errors.forEach(x => {
				AMessage.error(x.msg)
			})
		} else {
			AMessage.success(res.data.message)
		}
	})
}
// 上传头像
export const setAvatarUpload = fileItem => {
	const formData = new FormData()
	formData.append('file', fileItem.fileItem.file)
	// 修改头像路径
	const changeAvatar = avatarUrl => {
		return api.post('/users/changeAvatar', { avatarUrl }).then(res => {
			localStorage.setItem('avatarUrl', res.data.new_user.userAvatar)
			AMessage.success(res.data.message)
			return res.data.new_user
		})
	}
	return api.post('/admin/upload', formData).then(res => {
		return changeAvatar(res.data.url)
	})
}
// 发送站内信
export const setSendMail = mailModel => {
	return api.post('/users/sendEmail', mailModel).then(res => {
		if (res.data.status === 1) {
			res.data.errors.forEach(x => {
				AMessage.error(x.msg)
			})
		} else {
			AMessage.success(res.data.massage)
		}
	})
}
// 获取个人头像路径
export const getAvatarUrl = id => {
	return api.get(`/index/userAvatar/${id}`).then(res => {
		return res.data.find_userAvatar[0].userAvatar
	})
}
// 获取个人空间信息
export const getUserSpace = id => {
	return api.get(`/index/getUsersData/${id}`).then(res => {
		return res.data.data
	})
}
const usersApi = {
	// 获取用户历史评论
	getComment: () => {
		return api.get('/users/comment').then(res => {
			// 后续需要做一下排序
			return res.data.find_Comment
		})
	},
	// 获取站内信
	getMailList: () => {
		return api.get('/users/showEmail').then(res => {
			return res.data
		})
	},
	readMail: activeKey => {
		return api.get('/users/readEmail/' + activeKey)
	},
	// 获取收藏电影列表
	getFavoriteList: () => {
		return api.get('/users/favorite').then(res => {
			return res.data.find_favorite
		})
	},
	// 取消收藏
	delFavoriteMovie: (movie_id, movie_name) => {
		return api
			.post('/users/favorite', {
				movie_id: movie_id,
				movie_name: movie_name,
				favorite: false
			})
			.then(res => {
				return res.data
			})
	}
}
export default usersApi
